package org.jeecg.codegeneweb.template.ext;

import cn.hutool.core.util.StrUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.Data;
import org.jeecg.codegeneweb.template.DestinationSingleFileDomain;
import org.jeecg.codegeneweb.template.TemplateLocaltion;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

@Data
public class ExtTemplateSingleFileDomain {
    private File exttemplatefile;// file "D:\\book20241011\\jeecg\\JeecgBoot-3.7.1\\jeecg-boot\\jeecg-module-system\\jeecg-system-biz\\src\\main\\resources\\jeecg\\code-template-online\\default\\one\\java";
    private String exttemplatefilename;//"D:\\book20241011\\jeecg\\JeecgBoot-3.7.1\\jeecg-boot\\jeecg-module-system\\jeecg-system-biz\\src\\main\\resources\\jeecg\\code-template-online\\default\\one\\java";
    private String fixpart="\\${bussiPackage}\\${entityPackage}";
    private String controlentitymapperservicepath;
//    private boolean isDir=false;
//    private String originalRelativePath;//这个地址，替换之后需要与新的目标地址进行整合
//    private String originalOnlyFileName;
//    private String originalAbsolutePath;
//    private String afterreplaceRelativePath;
//    private String afterreplaceOnlyFileName;
//    private String afterreplaceAbsolutePath;


    private ExtTemplateLocaltion exttemplateLocaltion;
    private ExtDestinationSingleFileDomain extdestinationSingleFileDomain;

//
//    public boolean isDir() {
//        return isDir;
//    }
//
//    public void setDir(boolean dir) {
//        isDir = dir;
//    }
//
//    public String getOriginalRelativePath() {
//        String filename=getTemplatefile().getAbsolutePath().substring(getTemplatefilename().length(), getTemplatefile().getAbsolutePath().length());
//        System.out.println(filename);
//        return filename;
//    }
//
//    public void setOriginalRelativePath(String originalRelativePath) {
//        this.originalRelativePath = originalRelativePath;
//    }
//
//    public String getOriginalOnlyFileName() {
//        return getTemplatefile().getName();
//    }
//
//    public void setOriginalOnlyFileName(String originalOnlyFileName) {
//        this.originalOnlyFileName = originalOnlyFileName;
//    }
//
//    public String getOriginalAbsolutePath() {
//        return getTemplatefile().getAbsolutePath();
//    }
//
//    public void setOriginalAbsolutePath(String originalAbsolutePath) {
//        this.originalAbsolutePath = originalAbsolutePath;
//    }
//
//    public String getAfterreplaceRelativePath() {
//        return afterreplaceRelativePath;
//    }
//
//    public void setAfterreplaceRelativePath(String afterreplaceRelativePath) {
//        this.afterreplaceRelativePath = afterreplaceRelativePath;
//    }
//
//    public String getAfterreplaceOnlyFileName() {
//        return afterreplaceOnlyFileName;
//    }
//
//    public void setAfterreplaceOnlyFileName(String afterreplaceOnlyFileName) {
//        this.afterreplaceOnlyFileName = afterreplaceOnlyFileName;
//    }
//
//    public String getAfterreplaceAbsolutePath() {
//        return afterreplaceAbsolutePath;
//    }
//
//    public void setAfterreplaceAbsolutePath(String afterreplaceAbsolutePath) {
//        this.afterreplaceAbsolutePath = afterreplaceAbsolutePath;
//    }
//
//    public DestinationSingleFileDomain getDestinationSingleFileDomain() {
//        return destinationSingleFileDomain;
//    }
//
////    public void setDestinationSingleFileDomain(DestinationSingleFileDomain destinationSingleFileDomain) {
////        this.destinationSingleFileDomain = destinationSingleFileDomain;
////        this.destinationSingleFileDomain.setTemplateSingleFileDomain(this);
////    }
//
//    public TemplateLocaltion getTemplateLocaltion() {
//        return templateLocaltion;
//    }
//
//    public void setTemplateLocaltion(TemplateLocaltion templateLocaltion) {
//        this.templateLocaltion = templateLocaltion;
//    }

    public static void main(String[] args) throws Exception {
        try {
            new File("d:\\tmp\\one\\two\\three").mkdirs();
            new File("d:\\tmp\\one\\two\\four").mkdirs();
        }catch (Exception ex){

        }

        Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);
        File destfile=new File("D:\\jeecg3\\src\\main\\java\\qingjiadan\\qingjiadan\\controller\\QingjiaDanController.javai");
        File templatefile=new File("D:\\book20241011\\jeecg\\JeecgBoot-3.7.1\\jeecg-boot\\jeecg-module-system\\jeecg-system-biz\\src\\main\\resources\\jeecg\\code-template-online\\default\\one\\java\\${bussiPackage}\\${entityPackage}\\controller");
        configuration.setDirectoryForTemplateLoading(templatefile.getParentFile());

        // 获取模板
        Template templatereplacecode=configuration.getTemplate(templatefile.getName());//"template.ftl"
        templatereplacecode.setOutputEncoding("UTF-8");
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destfile), "UTF-8"));
        //template.process(map, bufferedWriter);
        bufferedWriter.close();
//        boolean isDir= StrUtil.subAfter(destfile.getName(),".",true).equals("");
//        if(!isDir){
//            if(!destfile.getParentFile().exists()) {
//                destfile.getParentFile().mkdirs();
//            }
//            //然后对这个文件开始进行替换操作
//            destfile.createNewFile();
//            // 设置模板目录
//            configuration.setDirectoryForTemplateLoading(file.getParentFile());
//
//            // 获取模板
//            Template templatereplacecode=configuration.getTemplate(file.getName());//"template.ftl"
//            templatereplacecode.setOutputEncoding("UTF-8");
//            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destfile), "UTF-8"));
//            template.process(map, bufferedWriter);
//            bufferedWriter.close();
//
//        }
    }



}
